<!-- 商品发布 - 库存价格 - 添加属性 -->
<template>
  <Dialog v-model="dialogVisible" title="修改有效期">
    <el-form
      ref="formRef"
      v-loading="formLoading"
      :model="formData"
      :rules="formRules"
      label-width="80px"
      @keydown.enter.prevent="submitForm"
    >
      <el-form-item label="有效期" prop="validity">
        <el-input v-model="formData.validity" placeholder="有效期" class="!w-320px">
          <template #prepend>
            <el-select
              v-model="formData.validityChangeType"
              class="w-80px!"
              @change="validityChangeTypeChange()"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.VALIDITY_TIME_CHANGE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </template>
          <template #append>
            <el-select
              v-model="formData.validityUnit"
              class="w-80px!"
              :disabled="formData.validityChangeType != 0"
            >
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.TENANT_DATE_UNIT)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </template>
        </el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script lang="ts" setup>
// import * as PropertyApi from '@/api/mall/product/property'
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'

defineOptions({ name: 'ValidityTimeChangeForm' })

// const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const formLoading = ref(false) // 表单的加载中
const formData = ref({
  validityChangeType: 0,
  validity: '',
  validityUnit: 0
})
const unit = ref(0) //用来标记原始的单位 增加减少使用原有单位
const formRules = reactive({
  validity: [
    { required: true, message: '时间不能为空', trigger: 'blur' },
    {
      pattern: /^[1-9]\d*$/,
      message: '请输入正整数',
      trigger: 'blur'
    }
  ]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (validityUnit) => {
  dialogVisible.value = true
  resetForm()
  unit.value = validityUnit
  formData.value.validityUnit = validityUnit
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/**有效期类型变更 */
const validityChangeTypeChange = () => {
  if (formData.value.validityChangeType != 0) {
    formData.value.validityUnit = unit.value
  }
}
/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调

const submitForm = async () => {
  // // 校验表单
  // if (!formRef) return
  // const valid = await formRef.value.validate()
  // if (!valid) return
  // // 提交请求
  // formLoading.value = true
  // try {
  //   const data = formData.value as PropertyApi.PropertyVO
  //   const propertyId = await PropertyApi.createProperty(data)
  //   // 添加到属性列表
  //   attributeList.value.push({
  //     id: propertyId,
  //     ...formData.value,
  //     values: []
  //   })
  //   message.success(t('common.createSuccess'))
  // emit('success')
  //   dialogVisible.value = false
  // } finally {
  //   formLoading.value = false
  // }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    validityChangeType: 0,
    validity: '',
    validityUnit: 0
  }
  formRef.value?.resetFields()
}
</script>
